package controller.library
{
	import events.library.EventRemoveLibraryItems;
	
	import model.ModelLibrary;
	import model.ModelProject;
	import model.ModelRooms;
	
	import org.flashIso.engine.base.IsoRoom;
	import org.flashIso.engine.base.IsoTiledMap;
	import org.flashIso.engine.library.objects.LBinaryData;
	import org.flashIso.engine.library.objects.LImage;
	import org.flashIso.engine.library.objects.LObject;
	import org.flashIso.engine.library.objects.LTile;
	import org.flashIso.engine.objects.IsoObject;
	import org.flashIso.engine.objects.IsoTile;
	import org.robotlegs.mvcs.Command;
	
	public class CommandRemoveLibraryItem extends Command
	{
		
		[Inject]
		public var event:EventRemoveLibraryItems;
		
		[Inject]
		public var ml:ModelLibrary;
		
		[Inject]
		public var mp:ModelProject;
		
		[Inject]
		public var mr:ModelRooms;
		
		override public function execute() : void {
			mp.projectModified = true;

			var i:uint;
			for (i = 0; i <event.itemsToRemove.length; i++){
				if (event.itemsToRemove[i] is LObject){
					removeObjectReference(event.itemsToRemove[i] as LObject);
					ml.removeLObject(event.itemsToRemove[i] as LObject);
				}else
				if (event.itemsToRemove[i] is LImage){
					removeImageReference(event.itemsToRemove[i] as LImage);
					ml.removeLImage(event.itemsToRemove[i] as LImage);
				}else
				if (event.itemsToRemove[i] is LTile){
					removeTileReference(event.itemsToRemove[i] as LTile);
					ml.removeLTile(event.itemsToRemove[i] as LTile);
				}else
				if (event.itemsToRemove[i] is LBinaryData){
					ml.removeLBinaryData(event.itemsToRemove[i] as LBinaryData);
				}else{
					throw(new Error("Unhandeled item to remove!"));
				}
			}
		}
		
		private function removeObjectReference(object:LObject) : void {
			var i:uint;
			var j:uint;
			for (i = 0; i < mr.roomsDataProvider.length; i++){
				var map:IsoTiledMap = (mr.roomsDataProvider[i] as IsoRoom).map;
				for (j = 0; j < map.numChildren; j++){
					if (map.getChildAt(j) is IsoObject){
						if ((map.getChildAt(j) as IsoObject).libraryReference == object){
							map.removeElement(map.getChildAt(j) as IsoObject);
							j--;
						}
					}
				}
			}
		}
		private function removeTileReference(object:LTile) : void {
			var i:uint;
			var j:uint;
			for (i = 0; i < mr.roomsDataProvider.length; i++){
				var map:IsoTiledMap = (mr.roomsDataProvider[i] as IsoRoom).map;
				for (j = 0; j < map.numChildren; j++){
					if (map.getChildAt(j) is IsoTile){
						if ((map.getChildAt(j) as IsoTile).libraryReference == object){
							map.removeElement(map.getChildAt(j) as IsoTile);
							j--;
						}
					}
				}
			}
		}
		
		private function removeImageReference(image:LImage) : void {
			var i:uint;
			for (i = 0; i < ml.tilesDataProvider.length; i++){
				if ((ml.tilesDataProvider[i] as LTile).image == image){
					(ml.tilesDataProvider[i] as LTile).image = null;
				}
			}
			for (i = 0; i < ml.objectsDataProvider.length; i++){
				if ((ml.objectsDataProvider[i] as LObject).image == image){
					(ml.objectsDataProvider[i] as LObject).image = null;
				}
			}
		}
	}
}